package com.rockcent.mall.lib.repository;

import com.rockcent.mall.lib.domain.ProductQuantity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.stereotype.Repository;

import javax.persistence.LockModeType;


@Repository
public interface ProductQuantityRepository extends JpaRepository<ProductQuantity, Long>, JpaSpecificationExecutor {


    /**
     * 暂时使用行锁去实现库存更新，当并发量大的时候就需要换方案
     * 注意一点，实现行锁必须是主键查询，非主键会导致锁表
     * @param id
     * @return
     */
    @Lock(LockModeType.PESSIMISTIC_WRITE)
    ProductQuantity findOne(Long id);


    ProductQuantity findById(Long productQuantityId);

    ProductQuantity findTop1ByMallIdAndMerchantIdAndProductId(Long mallId, Long merchantId, Long productId);
}
